﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MINERP.DAL.Statistics;

namespace MINERP.Statistics.Forms
{
    public partial class FrmSalesAchievementStat : Form
    {
        public FrmSalesAchievementStat()
        {
            InitializeComponent();
        }

        private void FrmSalesAchievementStat_Load(object sender, EventArgs e)
        {
            //初始化时间
            DEStart.DateTime = DateTime.Now.Date;
            DEEnd.DateTime = DateTime.Now.AddDays(1).Date;
            //DataSet ds = SalesAchievementStatDAL.GetAllSaleMan();
            //lookUpEdit1.Properties.DataSource = ds.Tables[0].DefaultView;
            //lookUpEdit1.Properties.DisplayMember = "Salesman";
            //lookUpEdit1.Properties.ValueMember = "Salesman";
            comboBoxEdit1.Properties.Items.Add("全部");
            foreach (string saleMan in SalesAchievementStatDAL.GetAllSaleMan())
            {
                comboBoxEdit1.Properties.Items.Add(saleMan);
            }

            comboBoxEdit1.SelectedIndex = 0;
        }

        private void BTNOK_Click(object sender, EventArgs e)
        {
            BTNOK.Enabled = false;
            int LookType = 6;//截取时间字符串的位数
            this.Cursor = Cursors.WaitCursor;
            string strSaleMan = " ";
            if (comboBoxEdit1.Text.Trim()!="全部")
            {
                strSaleMan = comboBoxEdit1.Text;
            }

            if (comboBoxEdit2.Text.Trim()=="日")
            {
                LookType = 8;
            }
            else if (comboBoxEdit2.Text.Trim() == "月")
            {
                LookType = 6;
            }
            else
            {
                LookType = 4;
            }

            DataSet dsResult = SalesAchievementStatDAL.GetSaleStat(strSaleMan, DEStart.DateTime.Date, DEEnd.DateTime.Date.AddDays(1),LookType);
            dsResult.WriteXml(@"C:\windows\xmlData\销售人员业绩统计报表.xml", XmlWriteMode.WriteSchema);

            report1.RegisterData(dsResult);
            //report1.SetParameterValue("startTime", DEStart.DateTime);
            //report1.SetParameterValue("EndTime", DEEnd.DateTime);

            report1.Load(Application.StartupPath + "\\销售人员业绩统计报表.frx");
            //report1.Parameters.FindByName("startTime").Expression = DEStart.DateTime.ToString();
            //report1.Parameters.FindByName("EndTime").Expression = DEEnd.DateTime.ToString();
            report1.SetParameterValue("startTime", DEStart.DateTime);
            report1.SetParameterValue("EndTime", DEEnd.DateTime);
            report1.Preview = previewControl1;
            previewControl1.Refresh();

            report1.Show();
            this.Cursor = Cursors.Default;
            BTNOK.Enabled = true;
        }
    }
}
